package com.whmnx.my.ui.viewmodel;

import android.app.Application;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.whmnx.base.bean.TolalBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.fragment.MyTotalSalesFragment;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/24 0024 15:29
 * version: 1.0
 * @author Administrator
 */
public class TotalModel extends BaseViewModel {


    public ObservableField<String> sales = new ObservableField<>("");
    private MyTotalSalesFragment mFragment;

    public TotalModel(@NonNull Application application) {
        super(application);

    }

    public void initToolbar(MyTotalSalesFragment myTotalSalesFragment) {
        this.mFragment = myTotalSalesFragment;
        initView();

    }

    private void initView() {
        Map<String, Object> params = new HashMap<>();
        params.put("mId", SPUtils.getInstance().getString(Constants.USERID));
        params.put("type","0");
        params.put("value","");
        params.put("pageIndex",1);
        params.put("pageSize",10);
        HttpClient.post(mFragment.getActivity(), Constants.SALESLIST, params,new CallBack<TolalBean>() {
            @Override
            public void onSuccess(TolalBean result) {
                sales.set("￥"  + result.getSales());
                observableList.clear();
                for (TolalBean.ListBean listBean : result.getList()) {
                    observableList.add(new TotalItemViewModel(TotalModel.this,listBean));
                }
            }
        });
    }


    //给RecyclerView添加ObservableList
    public ObservableList<TotalItemViewModel> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<TotalItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_total);

    //全部
    public BindingCommand OnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
           // showTime();
            showTime2();
        }
    });

    private void showTime2() {
        Dialog    dialogHead = new Dialog(mFragment.getActivity(), R.style.ActionSheetDialogStyle);
        View view = LayoutInflater.from(mFragment.getActivity()).inflate(R.layout.dialog_time, null);


        dialogHead.setContentView(view);
        Window window = dialogHead.getWindow();
        if (window == null) {
            return;
        }
        //宽高最大
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        //显示对话框//设置Dialog从窗体底部弹出
        dialogHead.setCanceledOnTouchOutside(true);
        dialogHead.show();
        window.setGravity(Gravity.BOTTOM);
        //获取窗体的属性
        WindowManager.LayoutParams lp = window.getAttributes();
        //设置Dialog距离底部的距离
        //将属性设置给窗体
        window.setAttributes(lp);


    }

    private void showTime() {
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 1, 1);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2030, 1, 1);

        TimePickerView pvTime = new TimePickerBuilder(mFragment.getActivity(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
                   /* try {
                        Date  mtime1 = sf.parse(Utils.getDayTime());
                        Date  mtime2 = sf.parse(Utils.getTime2(date));
                        if (mtime2.getTime() > mtime1.getTime()){
                            tvWordTime.setText(Utils.getTime2(date));
                        }else {
                            Utils.showToastSafe("开始日期须大于今天日期");
                            return;
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }*/
            }
        })
                .setType(new boolean[]{true, true, true, false, false, false})//默认
                .setTitleText("选择时间")
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setSubCalSize(16)//确定和取消文字大小
                .setSubmitColor(0xffFF6767)//确定按钮文字颜色
                .setCancelColor(0xff999999)//取消按钮文字颜色
                .setTitleBgColor(0xffF4F4F4)//标题背景颜色 Night mode
                .setTitleSize(16)
                .setContentTextSize(14)//滚轮文字大小
                .setTextColorCenter(0xff333333)
                .isCyclic(true)//是否循环滚动
                .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)//起始终止年月日设定
                .setTextColorOut(0x999999)
                .setDividerColor(0xffEEEEEE)
                .setLabel("年", "月", "日", "时", "分", "秒")
                .setOutSideCancelable(true)//点击外部dismiss default true
                .isDialog(false)//是否显示为对话框样式
                .setLineSpacingMultiplier((float) 3) //设置item的高度
                .build();
        //注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.setDate(Calendar.getInstance());
        pvTime.show();
    }
}
